package com.nlscan.qdkit.ui.publico.util;

import android.animation.ValueAnimator;
import android.view.View;

import com.quvii.qvlib.util.LogUtil;
import com.quvii.qvlib.util.ScreenUtil;

import java.lang.ref.WeakReference;

public class AutoMoveHelper {
    private int bottomHeight;
    private int topHeight;
    private int currentHeight;

    private WeakReference<View> weakReference;
    private int value = ScreenUtil.dip2px(100);
    private long time = 300L;
    private ValueAnimator valueAnimator = new ValueAnimator();

    public AutoMoveHelper(View view) {
        weakReference = new WeakReference<>(view);
        view.post(() -> {
            bottomHeight = view.getBottom();
            currentHeight = bottomHeight;
            topHeight = bottomHeight - value;

            LogUtil.i("bottom: " + bottomHeight);
        });

        valueAnimator.addUpdateListener(animation -> {
            View view1 = weakReference.get();

            if (view1 == null) {
                LogUtil.i("release");
                stop();
                return;
            }

            currentHeight = (int) animation.getAnimatedValue();

            view1.getLayoutParams().height = currentHeight;
            view1.requestLayout();
        });

        valueAnimator.setDuration(time);
    }

    public void moveTop() {
        valueAnimator.setIntValues(currentHeight, topHeight);
        valueAnimator.start();
    }

    public void moveBottom() {
        valueAnimator.setIntValues(currentHeight, bottomHeight);
        valueAnimator.start();
    }

    public void stop() {
        valueAnimator.cancel();
    }

    public void release() {
        stop();
    }
}
